package commandAction;

import java.awt.Point;
import java.util.ArrayList;

import com.supermap.data.Point2D;
import com.supermap.desktop.Application;
import com.supermap.desktop.Interface.IFormMap;
import com.supermap.mapping.Map;

import command.Command;
import command.Result;
import command.SPoint;

public class ActionMap extends ActionForm {

	public Object action(Command cmd) {
		Point[] points = null;
		Map map = null;
		if (cmd.getTitle() == null) {
			IFormMap formMap = (IFormMap) Application.getActiveApplication()
					.getActiveForm();
			map = formMap.getMapControl().getMap();
		}

		if (cmd.getValues() != null) {
			points = convertToPoint(map, cmd.getValues());
		}
		Result result = new Result();
		result.setPoints(points);
		return result;
	}

	public static Point[] convertToPoint(Map map, Object[] objects) {
		if (objects == null) {
			return null;
		}
		Point[] points = new Point[objects.length];
		for (int i = 0; i < objects.length; i++) {
			if (objects[i] instanceof SPoint) {
				SPoint sPoint = (SPoint) objects[i];
				Point2D point2D = new Point2D(sPoint.getX(), sPoint.getY());
				Point point = map.mapToPixel(point2D);
				points[i]=point;
			}
		}
		return points;
	}
}
